<?php

namespace CNEPAdmin\Controller;

use Think\Controller;

class LoginController extends Controller {

    public function index() {
        $this->display();
    }

    /**
     * 登录
     */
     public function checkLogin() {
        $rt = true;

        $data['account'] = trim(I("post.username"));
        $psw = trim(I("post.password"));

        $user = D("SysAdmin");
        $admin = $user->where($data)->find();

        if ($admin) {

            if ($admin['password'] === $psw) {
                // 如果不是超级管理员判断角色是否激活
                $manType = $admin['manType'];

                if (!(intval($manType) === 1)) {
                    $roleArr = D("SysAdminRole")->getRoleById($admin['roleID']);

                    if ($roleArr) {
                        if (!(intval($roleArr['isEnabled']) === 1)) {
                            $rt = false;
                            $rejson = array(
                                'result' => 0,
                                'desc' => "角色已禁用，请联系超级管理员以激活角色"
                            );
                        } else if (!(intval($roleArr['isOn']) === 1)) {
                            $rt = false;
                            $rejson = array(
                                'result' => 0,
                                'desc' => "角色已删除"
                            );
                        }
                    } else {
                        $rt = false;
                        $rejson = array(
                            'result' => 0,
                            'desc' => "角色不存在"
                        );
                    }
                }

                if ($rt) {

                    $_POST['adminID'] = $admin['adminID'];

                    if ($user->create(I("post."), 4)) {

                        if ($user->save()) {

                            $_SESSION['adminid'] = $admin['adminID'];
                            unset($admin['password']);
                            $_SESSION['adminArr'] = $admin;

                            $rejson = array(
                                'result' => 1,
                                'url' => U("CNEPAdmin/Index/index")
                            );
                        } else {
                            $rejson = array(
                                'result' => 0,
                                'desc' => $user->getDbError() . $user->getError()
                            );
                        }
                    } else {
                        $rejson = array(
                            'result' => 0,
                            'desc' => $user->getError()
                        );
                    }
                }
            } else {
                $rejson = array(
                    'result' => 0,
                    'desc' => '密码错误',
                );
            }
        } else {
            $rejson = array(
                'result' => 0,
                'desc' => '用户名不存在',
            );
        }

        $this->ajaxReturn($rejson);
    }

    /**
     * 注销
     */
    public function logout() {
        unset($_SESSION['adminid']);
        redirect(U('CNEPAdmin/Login/index'));
    }

}
